home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Tools / Languages / GCC 1.37.1r14 / usr / gcc-1.37.1r14 / non-obj files / ar.c next >
Encoding:
C/C++ Source or Header  |  1993-11-10  |  40.1 KB  |  1,779 lines  |  [TEXT/CPED]

  1. #define fatal ar_fatal
  2. #define error ar_error
  3. #define progname ar_progname
  4. #define main ar_main
  5. #define concat ar_concat
  6. #define mywrite ar_mywrite
  7. #define perror_with_name ar_perror_with_name
  8. #define pfatal_with_name ar_pfatal_with_name
  9.  
  10. /* ar.c - Archive modify and extract.
  11.    Copyright (C) 1988, 1990 Free Software Foundation, Inc.
  12.  
  13.    This program is free software; you can redistribute it and/or modify
  14.    it under the terms of the GNU General Public License as published by
  15.    the Free Software Foundation; either version 1, or (at your option)
  16.    any later version.
  17.  
  18.    This program is distributed in the hope that it will be useful,
  19.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  20.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  21.    GNU General Public License for more details.
  22.  
  23.    You should have received a copy of the GNU General Public License
  24.    along with this program; if not, write to the Free Software
  25.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  26.  
  27. #include <stdio.h>
  28. #include <ar.h>
  29. #include <errno.h>
  30. #include <sys/types.h>
  31.  
  32. #if !defined(A_OUT) && !defined(MACH_O)
  33. #define A_OUT
  34. #endif
  35.  
  36. #ifdef A_OUT
  37. #ifdef COFF_ENCAPSULATE
  38. #include "a.out.encap.h"
  39. #else
  40. #include <a.out.h>
  41. #endif
  42. #endif
  43.  
  44. #ifdef MACH_O
  45. #ifndef A_OUT
  46. #include <nlist.h>
  47. #endif
  48. #include <sys/loader.h>
  49. #endif
  50.  
  51. #if !defined(USG) || defined(hpux)
  52. #define HAVE_FCHMOD
  53. #ifndef THINK_C
  54. #define HAVE_FSYNC
  55. #endif
  56. #define HAVE_RENAME
  57. #endif /* !USG || hpux */
  58.  
  59. #ifdef USG
  60. #include <time.h>
  61. #include <fcntl.h>
  62. #else
  63. #include <sys/file.h>
  64. #include <sys/time.h>
  65. #endif
  66.  
  67. #ifdef    __GNUC__
  68. #define    alloca    __builtin_alloca
  69. #else
  70. # ifdef sparc
  71. #  include <alloca.h>
  72. # else
  73. #ifndef THINK_C
  74. char *alloca ();
  75. #endif
  76. # endif
  77. #endif
  78.  
  79. #ifdef    USG
  80. #define    bcopy(source, dest, size)    memcpy((dest), (source), (size))
  81. #define    bcmp(a, b, size)        memcmp((a), (b), (size))
  82. #define    bzero(s, size)            memset((s), 0, (size))
  83. #endif
  84.  
  85. /* Locking is normally disabled because fcntl hangs on the Sun
  86.    and it isn't supported properly across NFS anyway.  */
  87. #ifdef LOCKS
  88. /* You might need to compile with -I/usr/include/sys if your fcntl.h
  89.    isn't in /usr/include (which is where it should be according to POSIX).  */
  90. #include <fcntl.h>
  91. #endif
  92.  
  93. /* This structure is used internally to represent the info
  94.    on a member of an archive.  This is to make it easier to change format.  */
  95.  
  96. struct member_desc
  97.   {
  98.     /* Name of member.  */
  99.     char *name;
  100.  
  101.     /* The following fields are stored in the member header as decimal or octal
  102.        numerals, but in this structure they are stored as machine numbers.  */
  103.     int mode;        /* Protection mode from member header.  */
  104.     long int date;    /* Last modify date as stored in member header.  */
  105.     unsigned int size;    /* Bytes of member's data, from member header.  */
  106.     int uid, gid;    /* UID and GID fields copied from member header.  */
  107.     unsigned int offset;/* Offset in archive of the header of this member.  */
  108.     unsigned int data_offset;/* Offset of first data byte of the member.  */
  109.  
  110.     /* The next field does not describe where the member was in the
  111.        old archive, but rather where it will be in the modified archive.
  112.        It is set up by write_archive.  */
  113.     unsigned int new_offset;    /* Offset of this member in new archive */
  114.  
  115.     /* Symdef data for member.  Used only for files being inserted.  */
  116.     struct symdef *symdefs;
  117.     unsigned int nsymdefs;    /* Number of entries of symdef data.  */
  118.     unsigned int string_size;    /* Size of strings needed by symdef data.  */
  119.   };
  120.  
  121. /* Each symbol is recorded by something like this.  */
  122.  
  123. struct symdef
  124.   {
  125.     union
  126.       {
  127.     unsigned long int stringoffset;
  128.     char *name;
  129.       } s;
  130.     unsigned long int offset;
  131.   };
  132.  
  133. /* Nonzero means it's the name of an existing member;
  134.    position new or moved files with respect to this one.  */
  135.  
  136. char *posname;
  137.  
  138. /* How to use `posname':
  139.    POS_BEFORE means position before that member.
  140.    POS_AFTER means position after that member.
  141.    POS_DEFAULT if position by default; then `posname' should also be zero. */
  142.  
  143. enum { POS_DEFAULT, POS_BEFORE, POS_AFTER } postype;
  144.  
  145. /* Nonzero means describe each action performed.  */
  146.  
  147. int verbose;
  148.  
  149. /* Nonzero means don't warn about creating the archive file if necessary.  */
  150.  
  151. int silent_create;
  152.  
  153. /* Nonzero means don't replace existing members whose
  154.    dates are more recent than the corresponding files.  */
  155.  
  156. int newer_only;
  157.  
  158. /* Nonzero means preserve dates of members when extracting them.  */
  159.  
  160. int preserve_dates;
  161.  
  162. /* Operation to be performed.  */
  163.  
  164. #define DELETE 1
  165. #define REPLACE 2
  166. #define PRINT_TABLE 3
  167. #define PRINT_FILES 4
  168. #define EXTRACT 5
  169. #define MOVE 6
  170. #define QUICK_APPEND 7
  171.  
  172. int operation;
  173.  
  174. /* Name of archive file.  */
  175.  
  176. char *archive;
  177.  
  178. /* Descriptor on which we have locked the original archive file,
  179.    or -1 if this has not been done.  */
  180.  
  181. int lock_indesc;
  182.  
  183. /* Pointer to tail of `argv', at first subfile name argument,
  184.  or zero if no such were specified.  */
  185.  
  186. char **files;
  187.  
  188. /* Nonzero means write a __.SYMDEF member into the modified archive.  */
  189.  
  190. int symdef_flag;
  191.  
  192. /* Nonzero means __.SYMDEF member exists in old archive.  */
  193.  
  194. int symdef_exists;
  195.  
  196. /* Nonzero means don't update __.SYMDEF unless the flag was given.  */
  197.  
  198. int ignore_symdef;
  199.  
  200. /* Total number of symdef entries we will have. */
  201.  
  202. unsigned long int nsymdefs;
  203.  
  204. /* Symdef data from old archive (set up only if we need it) */
  205.  
  206. struct symdef *old_symdefs;
  207.  
  208. /* Number of symdefs in remaining in old_symdefs.  */
  209.  
  210. unsigned int num_old_symdefs;
  211.  
  212. /* Number of symdefs old_symdefs had when it was read in.  */
  213.  
  214. unsigned long int original_num_symdefs;
  215.  
  216. /* String table from old __.SYMDEF member.  */
  217.  
  218. char *old_strings;
  219.  
  220. /* Size of old_strings */
  221.  
  222. unsigned long int old_strings_size;
  223.  
  224. /* String table to be written into __.SYMDEF member.  */
  225.  
  226. char *new_strings;
  227.  
  228. /* Size of new_strings */
  229.  
  230. unsigned long int new_strings_size;
  231.  
  232. /* An archive map is a chain of these structures.
  233.   Each structure describes one member of the archive.
  234.   The chain is in the same order as the members are.  */
  235.  
  236. struct mapelt
  237. {
  238.   struct member_desc info;
  239.   struct mapelt *next;
  240. };
  241.  
  242. struct mapelt *maplast;
  243.  
  244. /* If nonzero, this is the map-element for the __.SYMDEF member
  245.    and we should update the time of that member just before finishing.  */
  246.  
  247. struct mapelt *symdef_mapelt;
  248.  
  249. /* Header that we wrote for the __.SYMDEF member.  */
  250.  
  251. struct ar_hdr symdef_header;
  252.  
  253. /* Name this program was run with. */
  254. char *program_name;
  255.  
  256. #ifndef xmalloc
  257. char *xmalloc (), *xrealloc ();
  258. void free ();
  259. #endif
  260.  
  261. void add_to_map (), delete_from_map ();
  262. int insert_in_map ();
  263. void print_descr ();
  264. char *concat ();
  265. void scan ();
  266. void extract_members ();
  267. void extract_member ();
  268. void print_contents ();
  269. void write_symdef_member ();
  270. void two_operations ();
  271. void usage (), fatal (), error (), error_with_file ();
  272. void perror_with_name (), pfatal_with_name ();
  273. void write_archive ();
  274. void touch_symdef_member ();
  275. void update_symdefs ();
  276. void delete_members (), move_members (), replace_members ();
  277. void quick_append ();
  278.  
  279. /* Output BYTES of data at BUF to the descriptor DESC.
  280.    FILE is the name of the file (for error messages).  */
  281.  
  282. void
  283. mywrite (desc, buf, bytes, file)
  284.      int desc;
  285.      char *buf;
  286.      int bytes;
  287.      char *file;
  288. {
  289.   register int val;
  290.  
  291.   while (bytes > 0)
  292.     {
  293.       val = write (desc, buf, bytes);
  294.       if (val <= 0)
  295.     perror_with_name (file);
  296.       buf += val;
  297.       bytes -= val;
  298.     }
  299. }
  300.  
  301. void
  302. main (argc, argv)
  303.      int argc;
  304.      char **argv;
  305. {
  306.   int i;
  307.  
  308.   operation = 0;
  309.   verbose = 0;
  310.   newer_only = 0;
  311.   silent_create = 0;
  312.   posname = 0;
  313.   postype = POS_DEFAULT;
  314.   preserve_dates = 0;
  315.   symdef_flag = 0;
  316.   symdef_exists = 0;
  317.   ignore_symdef = 0;
  318.   symdef_mapelt = 0;
  319.   files = 0;
  320.   lock_indesc = -1;
  321.  
  322.   if (argc < 2)
  323.     usage ("too few command arguments", 0);
  324.  
  325.   {
  326.     char *key = argv[1];
  327.     char *p = key;
  328.     char c;
  329.  
  330.     while (c = *p++)
  331.       {
  332.     switch (c)
  333.       {
  334.       case 'a':
  335.         postype = POS_AFTER;
  336.         break;
  337.  
  338.       case 'b':
  339.         postype = POS_BEFORE;
  340.         break;
  341.  
  342.       case 'c':
  343.         silent_create = 1;
  344.         break;
  345.  
  346.       case 'd':
  347.         if (operation)
  348.           two_operations ();
  349.  
  350.         operation = DELETE;
  351.         break;
  352.  
  353.       case 'i':
  354.         postype = POS_BEFORE;
  355.         break;
  356.  
  357.       case 'l':
  358.         break;
  359.  
  360.       case 'm':
  361.         if (operation)
  362.           two_operations ();
  363.         operation = MOVE;
  364.         break;
  365.  
  366.       case 'o':
  367.         preserve_dates = 1;
  368.         break;
  369.  
  370.       case 'p':
  371.         if (operation)
  372.           two_operations ();
  373.         operation = PRINT_FILES;
  374.         break;
  375.  
  376.       case 'q':
  377.         if (operation)
  378.           two_operations ();
  379.         operation = QUICK_APPEND;
  380.         break;
  381.  
  382.       case 'r':
  383.         if (operation)
  384.           two_operations ();
  385.         operation = REPLACE;
  386.         break;
  387.  
  388.       case 's':
  389.         symdef_flag = 1;
  390.         break;
  391.  
  392.       case 't':
  393.         if (operation)
  394.           two_operations ();
  395.         operation = PRINT_TABLE;
  396.         break;
  397.  
  398.       case 'u':
  399.         operation = REPLACE;
  400.         newer_only = 1;
  401.         break;
  402.  
  403.       case 'v':
  404.         verbose = 1;
  405.         break;
  406.  
  407.       case 'x':
  408.         if (operation)
  409.           two_operations ();
  410.         operation = EXTRACT;
  411.         break;
  412.       }
  413.       }
  414.   
  415.   }
  416.  
  417.   if (operation == 0 && symdef_flag)
  418.     operation = REPLACE;
  419.  
  420.   if (operation == 0)
  421.     usage ("no operation specified", 0);
  422.  
  423.   i = 2;
  424.  
  425.   if (postype != POS_DEFAULT)
  426.     posname = argv[i++];
  427.  
  428.   archive = argv[i++];
  429.  
  430.   if (i < argc)
  431.     {
  432.       files = &argv[i];
  433.       while (i < argc)
  434.     if (!strcmp (argv[i++], "__.SYMDEF"))
  435.       {
  436.         ignore_symdef = 1;
  437.         break;
  438.       }
  439.     }
  440.  
  441.   switch (operation)
  442.     {
  443.     case EXTRACT:
  444.     extract_members (extract_member);
  445.     break;
  446.  
  447.     case PRINT_TABLE:
  448.     extract_members (print_descr);
  449.     break;
  450.  
  451.     case PRINT_FILES:
  452.     extract_members (print_contents);
  453.     break;
  454.  
  455.     case DELETE:
  456.     if (files != 0)
  457.       delete_members ();
  458.     break;
  459.  
  460.     case MOVE:
  461.     if (files != 0)
  462.       move_members ();
  463.     break;
  464.  
  465.     case REPLACE:
  466.     if (files != 0 || symdef_flag)
  467.       replace_members ();
  468.     break;
  469.  
  470.     case QUICK_APPEND:
  471.     if (files != 0)
  472.       quick_append ();
  473.     break;
  474.  
  475.     default:
  476.     usage ("invalid operation %d", operation);
  477.     }
  478.  
  479.   exit (0);
  480. }
  481.  
  482. void
  483. two_operations ()
  484. {
  485.   usage ("two different operation switches specified", 0);
  486. }
  487.  
  488.  
  489. void
  490. scan (function, crflag)
  491.      void (*function) ();
  492.      int crflag;
  493. {
  494. }
  495.  
  496.  
  497. void print_modes ();
  498.  
  499. void
  500. print_descr (member)
  501.      struct member_desc member;
  502. {
  503.   char *timestring;
  504.   if (!verbose)
  505.     {
  506.       puts (member.name);
  507.       return;
  508.     }
  509.   print_modes (member.mode);
  510.   timestring = ctime (&member.date);
  511.   printf (" %2d/%2d %6d %12.12s %4.4s %s\n",
  512.       member.uid, member.gid,
  513.       member.size, timestring + 4, timestring + 20,
  514.       member.name);
  515. }
  516.  
  517. void
  518. print_modes (modes)
  519.      int modes;
  520. {
  521.   putchar (modes & 0400 ? 'r' : '-');
  522.   putchar (modes & 0200 ? 'w' : '-');
  523.   putchar (modes & 0100 ? 'x' : '-');
  524.   putchar (modes & 040 ? 'r' : '-');
  525.   putchar (modes & 020 ? 'w' : '-');
  526.   putchar (modes & 010 ? 'x' : '-');
  527.   putchar (modes & 04 ? 'r' : '-');
  528.   putchar (modes & 02 ? 'w' : '-');
  529.   putchar (modes & 01 ? 'x' : '-');
  530. }
  531.  
  532. #define BUFSIZE 1024
  533.  
  534. void
  535. extract_member (member, istream)
  536.      struct member_desc member;
  537.      FILE *istream;
  538. {
  539.   int ncopied = 0;
  540.   FILE *ostream;
  541.  
  542.   fseek (istream, member.data_offset, 0);
  543.   ostream = fopen (member.name, "w");
  544.   if (!ostream)
  545.     {
  546.       perror_with_name (member.name);
  547.       return;
  548.     }
  549.  
  550.   if (verbose)
  551.     printf ("x - %s\n", member.name);
  552.  
  553.   while (ncopied < member.size)
  554.     {
  555.       char buf [BUFSIZE];
  556.       int tocopy = member.size - ncopied;
  557.       int nread;
  558.       if (tocopy > BUFSIZE) tocopy = BUFSIZE;
  559.       nread = fread (buf, 1, tocopy, istream);
  560.       if (nread != tocopy)
  561.     fatal ("file %s not a valid archive", archive);
  562.       fwrite (buf, 1, nread, ostream);
  563.       ncopied += tocopy;
  564.     }
  565. #ifndef THINK_C
  566. #ifdef HAVE_FCHMOD
  567.   fchmod (fileno (ostream), member.mode);
  568. #else
  569.   chmod (member.name, member.mode);
  570. #endif
  571. #endif
  572.   if (ferror (ostream) || fclose (ostream) != 0)
  573.     error ("%s: I/O error", member.name);
  574. #ifndef THINK_C
  575.   if (preserve_dates)
  576.     {
  577. #ifdef USG
  578.       long tv[2];
  579.       tv[0] = member.date;
  580.       tv[1] = member.date;
  581.       utime (member.name, tv);
  582. #else
  583.       struct timeval tv[2];
  584.       tv[0].tv_sec = member.date;
  585.       tv[0].tv_usec = 0;
  586.       tv[1].tv_sec = member.date;
  587.       tv[1].tv_usec = 0;
  588.       utimes (member.name, tv);
  589. #endif
  590.     }
  591. #endif
  592. }
  593.  
  594. void
  595. print_contents (member, istream)
  596.      struct member_desc member;
  597.      FILE *istream;
  598. {
  599.   int ncopied = 0;
  600.  
  601.   fseek (istream, member.data_offset, 0);
  602.  
  603.   if (verbose)
  604.   printf ("\n<member %s>\n\n", member.name);
  605.  
  606.   while (ncopied < member.size)
  607.     {
  608.       char buf [BUFSIZE];
  609.       int tocopy = member.size - ncopied;
  610.       int nread;
  611.       if (tocopy > BUFSIZE) tocopy = BUFSIZE;
  612.       nread = fread (buf, 1, tocopy, istream);
  613.       if (nread != tocopy)
  614.     fatal ("file %s not a valid archive", archive);
  615.       fwrite (buf, 1, nread, stdout);
  616.       ncopied += tocopy;
  617.     }
  618. }
  619.  
  620. /* Make a map of the existing members of the archive: their names,
  621.  positions and sizes.  */
  622.  
  623. /* If `nonexistent_ok' is nonzero,
  624.  just return 0 for an archive that does not exist.
  625.  This will cause the ordinary supersede procedure to
  626.  create a new archive.  */
  627.  
  628. struct mapelt *
  629. make_map (nonexistent_ok)
  630.      int nonexistent_ok;
  631. {
  632.   struct mapelt mapstart;
  633.   mapstart.next = 0;
  634.   maplast = &mapstart;
  635.   scan (add_to_map, nonexistent_ok);
  636.   return mapstart.next;
  637. }
  638.  
  639. void
  640. add_to_map (member)
  641.      struct member_desc member;
  642. {
  643.   struct mapelt *mapelt = (struct mapelt *) xmalloc (sizeof (struct mapelt));
  644.   mapelt->info = member;
  645.   mapelt->info.name = concat (mapelt->info.name, "", "");
  646.   maplast->next = mapelt;
  647.   mapelt->next = 0;
  648.   maplast = mapelt;
  649. }
  650.  
  651. /* Return the last element of the specified map.  */
  652.  
  653. struct mapelt *
  654. last_mapelt (map)
  655.      struct mapelt *map;
  656. {
  657.   struct mapelt *tail = map;
  658.   while (tail->next) tail = tail->next;
  659.   return tail;
  660. }
  661.  
  662. /* Return the element of the specified map which precedes elt.  */
  663.  
  664. struct mapelt *
  665. prev_mapelt (map, elt)
  666.      struct mapelt *map, *elt;
  667. {
  668.   struct mapelt *tail = map;
  669.   while (tail->next && tail->next != elt)
  670.     tail = tail->next;
  671.   if (tail->next) return tail;
  672.   return 0;
  673. }
  674.  
  675. /* Return the element of the specified map which has the specified name.  */
  676.  
  677. struct mapelt *
  678. find_mapelt_noerror (map, name)
  679.      struct mapelt *map;
  680.      register char *name;
  681. {
  682.   register struct mapelt *tail;
  683.   unsigned int len = strlen (name);
  684.   int dot_o = name[len - 2] == '.' && name[len - 1] == 'o';
  685.  
  686.   for (tail = map; tail != 0; tail = tail->next)
  687.     {
  688.       if (tail->info.name == 0)
  689.     continue;
  690.       if (!strncmp (tail->info.name, name, 13))
  691.     {
  692.       unsigned int eltlen = strlen (tail->info.name);
  693.       if (len <= 13 || eltlen <= 13)
  694.         return tail;
  695.       else
  696.         {
  697.           char *p = tail->info.name + 13;
  698.           if (dot_o && p[0] == '.' && p[1] == 'o' && p[2] == '\0')
  699.         return tail;
  700.           else if (!strncmp (p, name + 13,
  701.                  (len > eltlen ? len : eltlen) - 13))
  702.         return tail;
  703.         }
  704.     }
  705.     }
  706.  
  707.   return 0;
  708. }
  709.  
  710. struct mapelt *
  711. find_mapelt (map, name)
  712.      struct mapelt *map;
  713.      char *name;
  714. {
  715.   register struct mapelt *found = find_mapelt_noerror (map, name);
  716.   if (found == 0)
  717.     error ("no member named `%s'", name);
  718.   return found;
  719. }
  720.  
  721. /* Write a new archive file from a given map.  */
  722. /* When a map is used as the pattern for a new archive,
  723.  each element represents one member to put in it, and
  724.  the order of elements controls the order of writing.
  725.  
  726.  Ordinarily, the element describes a member of the old
  727.  archive, to be copied into the new one.
  728.  
  729.  If the `offset' field of the element's info is 0,
  730.  then the element describes a file to be copied into the
  731.  new archive.  The `name' field is the file's name.
  732.  
  733.  If the `name' field of an element is 0, the element is ignored.
  734.  This makes it easy to specify deletion of archive members.
  735.  
  736.  Every operation that will eventually call `write_archive'
  737.  should call `lock_for_update' before beginning
  738.  to do any I/O on the archive file.
  739. */
  740.  
  741. void lock_for_update()
  742.     {
  743.     }
  744.  
  745. char *make_tempname ();
  746. void copy_out_member ();
  747.  
  748. void
  749. write_archive (map, appendflag)
  750.      struct mapelt *map;
  751.      int appendflag;
  752. {
  753.   char *tempname = make_tempname (archive);
  754.   int outdesc;
  755.   char *outname;
  756.   struct mapelt *tail;
  757.  
  758.   /* Now open the output.  */
  759.  
  760.   if (!appendflag)
  761.     {
  762.       /* Updating an existing archive normally.
  763.      Write output as TEMPNAME and rename at the end.
  764.      There can never be two invocations trying to do this at once,
  765.      because of the lock made on the old archive file.  */
  766.  
  767.       outdesc = open (tempname, O_WRONLY | O_CREAT, 0666);
  768.       if (outdesc < 0)
  769.     pfatal_with_name (tempname);
  770.       outname = tempname;
  771.       mywrite (outdesc, ARMAG, SARMAG, outname);
  772.     }
  773.   else
  774.     {
  775.       /* Fast-append to existing archive.  */
  776.  
  777.       outdesc = open (archive, O_WRONLY | O_APPEND, 0);
  778.       if (outdesc < 0)
  779.     pfatal_with_name (archive);
  780.       outname = archive;
  781.     }
  782.  
  783.   /* If archive has or should have a __.SYMDEF member,
  784.      compute the contents for it.  */
  785.  
  786.   if (symdef_flag || symdef_exists)
  787.     {
  788.     {
  789.       struct mapelt *this = (struct mapelt *)
  790.         xmalloc (sizeof (struct mapelt));
  791.       this->info.name = "__.SYMDEF";
  792.       this->info.offset = SARMAG;
  793.       this->info.data_offset = SARMAG + sizeof (struct ar_hdr);
  794.       this->info.new_offset = 0;
  795.       this->info.date = 0;
  796.       this->info.size = 0;
  797.       this->info.uid = 0;
  798.       this->info.gid = 0;
  799.       this->info.mode = 0666;
  800.       this->info.symdefs = 0;
  801.       this->info.nsymdefs = 0;
  802.       this->info.string_size = 0;
  803.       this->next = map;
  804.       map = this;
  805.       original_num_symdefs = 0;
  806.       old_strings_size = 0;
  807.     }
  808.  
  809.       update_symdefs (map, 0);
  810.     }
  811.  
  812.   /* Copy the members into the output, either from the old archive
  813.      or from specified files.  */
  814.  
  815.   for (tail = map; tail != 0; tail = tail->next)
  816.     {
  817.       if ((symdef_flag || symdef_exists) && tail->info.name
  818.       && !strcmp (tail->info.name, "__.SYMDEF")
  819. #if 0
  820.       && tail->info.date==0
  821. #endif
  822.       )
  823.     write_symdef_member (tail, map, outdesc, outname);
  824.       else
  825.     copy_out_member (tail, 0, outdesc, outname);
  826.     }
  827.  
  828.   if (symdef_mapelt != 0)
  829.     {
  830.       /* Check for members whose data offsets weren't
  831.      known when the symdef member was first written.  */
  832.       int doneany = 0;
  833.       for (tail = map; tail != 0; tail = tail->next)
  834.     if (tail->info.offset == 0)
  835.       {
  836.         /* Fix up the symdefs.  */
  837.         register unsigned int i;
  838.         for (i = 0; i < tail->info.nsymdefs; ++i)
  839.           tail->info.symdefs[i].offset = tail->info.new_offset;
  840.         doneany = 1;
  841.       }
  842.       if (doneany)
  843.     {
  844.       /* Some files had bad symdefs; rewrite the symdef member.  */
  845.       lseek (outdesc, symdef_mapelt->info.offset, 0);
  846.       write_symdef_member (symdef_mapelt, map, outdesc, outname);
  847.     }
  848.     }
  849.  
  850.   /* Mark the __.SYMDEF member as up to date.  */
  851.  
  852.   if (symdef_mapelt != 0)
  853.     touch_symdef_member (outdesc, outname);
  854.  
  855.   /* Install the new output under the intended name.  */
  856.  
  857. #ifdef HAVE_FSYNC
  858.   fsync (outdesc);
  859. #endif
  860.   close (outdesc);
  861. }
  862.  
  863. void
  864. header_from_map (header, mapelt)
  865.      struct ar_hdr *header;
  866.      struct mapelt *mapelt;
  867. {
  868.   unsigned int namelen;
  869.  
  870.   /* Zero the header, then store in the data as text.  */
  871.   bzero ((char *) header, sizeof (*header));
  872.  
  873.   strncpy (header->ar_name, mapelt->info.name, sizeof (header->ar_name));
  874.   namelen = strlen (mapelt->info.name);
  875.   if (namelen >= sizeof (header->ar_name))
  876.     {
  877.       if (mapelt->info.name[namelen - 2] == '.' &&
  878.       mapelt->info.name[namelen - 1] == 'o')
  879.     {
  880.       header->ar_name[sizeof (header->ar_name) - 3] = '.';
  881.       header->ar_name[sizeof (header->ar_name) - 2] = 'o';
  882.     }
  883.       header->ar_name[sizeof (header->ar_name) - 1] = '\0';
  884.       error ("member name `%s' truncated to `%s'",
  885.          mapelt->info.name, header->ar_name);
  886.     }
  887.  
  888.   sprintf (header->ar_date, "%ld", mapelt->info.date);
  889.   sprintf (header->ar_size, "%d", mapelt->info.size);
  890.   sprintf (header->ar_uid, "%d", mapelt->info.uid);
  891.   sprintf (header->ar_gid, "%d", mapelt->info.gid);
  892.   sprintf (header->ar_mode, "%o", mapelt->info.mode);
  893.   strncpy (header->ar_fmag, ARFMAG, sizeof (header->ar_fmag));
  894.  
  895.   /* Change all remaining nulls in the header into spaces.  */
  896.   {
  897.     char *end = (char *) &header[1];
  898.     register char *p;
  899.     for (p = (char *) header; p < end; ++p)
  900.       if (*p == '\0')
  901.     *p = ' ';
  902.   }
  903. }
  904.  
  905. /* writes to file open on OUTDESC with name OUTNAME.  */
  906. void
  907. copy_out_member (mapelt, archive_indesc, outdesc, outname)
  908.      struct mapelt *mapelt;
  909.      int archive_indesc;
  910.      int outdesc;
  911.      char *outname;
  912. {
  913.   struct ar_hdr header;
  914.   int indesc;
  915.  
  916.   if (mapelt->info.name == 0)
  917.     /* This element was cancelled.  */
  918.     return;
  919.  
  920.   header_from_map (&header, mapelt);
  921.  
  922.     {
  923.       indesc = open (mapelt->info.name, 0, 0);
  924.       if (indesc < 0)
  925.     {
  926.       perror_with_name (mapelt->info.name);
  927.       return;
  928.     }
  929.     }
  930.  
  931.   mywrite (outdesc, &header, sizeof (header), outname);
  932.  
  933.   if (mapelt->info.data_offset == 0)
  934.     mapelt->info.data_offset = lseek (outdesc, 0L, 1);
  935.  
  936.   {
  937.     char *buf = xmalloc(BUFSIZE);
  938.     int thistime = BUFSIZE;
  939.     while (thistime > 0)
  940.       {
  941.        thistime = read (indesc, buf, BUFSIZE);
  942.         mywrite (outdesc, buf, thistime, outname);
  943.       }
  944.     free(buf);
  945.   }
  946.  
  947.     close (indesc);
  948.  
  949.   if (mapelt->info.size & 1)
  950.     mywrite (outdesc, "\n", 1, outname);
  951. }
  952.  
  953. /* Update the time of the __.SYMDEF member; done when we updated
  954.    that member, just before we close the new archive file.
  955.    It is open on OUTDESC and its name is OUTNAME.  */
  956.  
  957. void
  958. touch_symdef_member (outdesc, outname)
  959.      int outdesc;
  960.      char *outname;
  961. {
  962. }
  963.  
  964. char *
  965. make_tempname (name)
  966.      char *name;
  967. {
  968.   return concat (name, "", "_supersede");
  969. }
  970.  
  971. void
  972. delete_members ()
  973. {
  974.   struct mapelt *map = make_map (0);
  975.   struct mapelt mapstart;
  976.   char **p;
  977.  
  978.   mapstart.info.name = 0;
  979.   mapstart.next = map;
  980.   map = &mapstart;
  981.  
  982.   lock_for_update ();
  983.  
  984.   if (files)
  985.     for (p = files; *p; p++)
  986.       {
  987.     /* If user says to delete the __.SYMDEF member,
  988.        don't make a new one to replace it.  */
  989.     if (!strcmp (*p, "__.SYMDEF"))
  990.       symdef_exists = 0;
  991.     delete_from_map (*p, map);
  992.       }
  993.  
  994.   write_archive (map->next, 0);
  995. }
  996.  
  997. void
  998. delete_from_map (name, map)
  999.      char *name;
  1000.      struct mapelt *map;
  1001. {
  1002.   struct mapelt *this = find_mapelt (map, name);
  1003.  
  1004.   if (!this) return;
  1005.   this->info.name = 0;
  1006.   if (verbose)
  1007.     printf ("d - %s\n", name);
  1008. }
  1009.  
  1010. void
  1011. move_members ()
  1012. {
  1013.   struct mapelt *map = make_map (0);
  1014.   char **p;
  1015.   struct mapelt *after_mapelt;
  1016.   struct mapelt mapstart;
  1017.   struct mapelt *change_map;
  1018.  
  1019.   mapstart.info.name = 0;
  1020.   mapstart.next = map;
  1021.   change_map = &mapstart;
  1022.  
  1023.   lock_for_update ();
  1024.  
  1025.   switch (postype)
  1026.     {
  1027.     case POS_DEFAULT:
  1028.       after_mapelt = last_mapelt (change_map);
  1029.       break;
  1030.  
  1031.     case POS_AFTER:
  1032.       after_mapelt = find_mapelt (map, posname);
  1033.       break;
  1034.  
  1035.     case POS_BEFORE:
  1036.       after_mapelt = prev_mapelt (change_map, find_mapelt (map, posname));
  1037.     }
  1038.  
  1039.   /* Failure to find specified "before" or "after" member
  1040.      is a fatal error; message has already been printed.  */
  1041.  
  1042.   if (!after_mapelt) exit (1);
  1043.  
  1044.   if (files)
  1045.     for (p = files; *p; p++)
  1046.       {
  1047.     if (move_in_map (*p, change_map, after_mapelt))
  1048.       after_mapelt = after_mapelt->next;
  1049.       }
  1050.  
  1051.   write_archive (map, 0);
  1052. }
  1053.  
  1054. int
  1055. move_in_map (name, map, after)
  1056.      char *name;
  1057.      struct mapelt *map, *after;
  1058. {
  1059.   struct mapelt *this = find_mapelt (map, name);
  1060.   struct mapelt *prev;
  1061.  
  1062.   if (!this) return 0;
  1063.   prev = prev_mapelt (map, this);
  1064.   prev->next = this->next;
  1065.   this->next = after->next;
  1066.   after->next = this;
  1067.   return 1;
  1068. }
  1069.  
  1070. /* Insert files into the archive.  */
  1071.  
  1072. void
  1073. replace_members ()
  1074. {
  1075.   struct mapelt *map = make_map (1);
  1076.   struct mapelt mapstart;
  1077.   struct mapelt *after_mapelt;
  1078.   struct mapelt *change_map;
  1079.   char **p;
  1080.   int changed;
  1081.  
  1082.   mapstart.info.name = 0;
  1083.   mapstart.next = map;
  1084.   change_map = &mapstart;
  1085.  
  1086.   lock_for_update ();
  1087.  
  1088.   switch (postype)
  1089.     {
  1090.     case POS_DEFAULT:
  1091.       after_mapelt = last_mapelt (change_map);
  1092.       break;
  1093.  
  1094.     case POS_AFTER:
  1095.       after_mapelt = find_mapelt (map, posname);
  1096.       break;
  1097.  
  1098.     case POS_BEFORE:
  1099.       after_mapelt = prev_mapelt (change_map, find_mapelt (map, posname));
  1100.     }
  1101.  
  1102.   /* Failure to find specified "before" or "after" member
  1103.      is a fatal error; the message has already been printed.  */
  1104.   if (after_mapelt == 0)
  1105.     exit (1);
  1106.  
  1107.   changed = 0;
  1108.   if (files != 0)
  1109.     for (p = files; *p != 0; ++p)
  1110.       if (insert_in_map (*p, change_map, after_mapelt))
  1111.     {
  1112.       after_mapelt = after_mapelt->next;
  1113.       changed = 1;
  1114.     }
  1115.  
  1116.   change_map = change_map->next;
  1117.   if (!changed && (!symdef_flag || symdef_exists))
  1118.     /* Nothing changed.  */ ;
  1119.   else
  1120.     write_archive (change_map, 0);
  1121. }
  1122.  
  1123. /* Handle the "quick insert" operation.  */
  1124.  
  1125. void
  1126. quick_append ()
  1127. {
  1128.   struct mapelt *map;
  1129.   struct mapelt *after;
  1130.   struct mapelt mapstart;
  1131.   char **p;
  1132.  
  1133.   mapstart.info.name = 0;
  1134.   mapstart.next = 0;
  1135.   map = &mapstart;
  1136.   after = map;
  1137.  
  1138.   lock_for_update ();
  1139.  
  1140.   /* Insert the specified files into the "map",
  1141.      but is a map of the inserted files only,
  1142.      and starts out empty.  */
  1143.   if (files)
  1144.     for (p = files; *p; p++)
  1145.       {
  1146.     if (insert_in_map (*p, map, after))
  1147.       after = after->next;
  1148.       }
  1149.  
  1150.   /* Append these files to the end of the existing archive file.  */
  1151.  
  1152.   write_archive (map->next, 1);
  1153. }
  1154.  
  1155. /* Insert an entry for name NAME into the map MAP after the map entry AFTER.
  1156.    Delete an old entry for NAME.
  1157.    MAP is assumed to start with a dummy entry, which facilitates
  1158.    insertion at the beginning of the list.
  1159.    Return 1 if successful, 0 if did nothing because file NAME doesn't
  1160.    exist or (optionally) is older.  */
  1161.  
  1162. int
  1163. insert_in_map (name, map, after)
  1164.      char *name;
  1165.      struct mapelt *map, *after;
  1166. {
  1167.   struct mapelt *old = find_mapelt_noerror (map, name);
  1168.   struct mapelt *this;
  1169.   if (old)
  1170.     /* Delete the old one.  */
  1171.     old->info.name = 0;
  1172.   this = (struct mapelt *) xmalloc (sizeof (struct mapelt));
  1173.   this->info.name = name;
  1174.   this->info.offset = 0;
  1175.   this->info.data_offset = 0;
  1176.   this->info.date = 0;
  1177.       {
  1178.       int fd = open(name, O_RDONLY);
  1179.       lseek(fd, 0, SEEK_END);
  1180.       this->info.size = tell(fd);
  1181.       close(fd);
  1182.       }
  1183.   this->info.uid = 0;
  1184.   this->info.gid = 0;
  1185.   this->info.mode = 0;
  1186.   /* Always place a __.SYMDEF member first in the archive, regardless
  1187.      of any position specifications.  */
  1188.   if (! strcmp (name, "__.SYMDEF"))
  1189.     this->next = map->next, map->next = this;
  1190.   else
  1191.     this->next = after->next, after->next = this;
  1192.  
  1193.   if (verbose)
  1194.     printf ("%c - %s\n", old == 0 ? 'a' : 'r', this->info.name);
  1195.  
  1196.   return 1;
  1197. }
  1198.  
  1199. /* Apply a function to each of the specified members.
  1200. */
  1201.  
  1202. void
  1203. extract_members (function)
  1204.      void (*function) ();
  1205. {
  1206.   struct mapelt *map;
  1207.   FILE *arcstream;
  1208.   char **p;
  1209.  
  1210.   if (!files)
  1211.     {
  1212.       /* Handle case where we want to operate on every member.
  1213.      No need to make a map and search it for this.  */
  1214.       scan (function, 0);
  1215.       return;
  1216.     }
  1217.  
  1218.   arcstream = fopen (archive, "r");
  1219.   if (!arcstream)
  1220.     fatal ("failure opening archive %s for the second time", archive);
  1221.   map = make_map (0);
  1222.  
  1223.   for (p = files; *p; p++)
  1224.     {
  1225.       struct mapelt *this = find_mapelt (map, *p);
  1226.       if (!this) continue;
  1227.       function (this->info, arcstream);
  1228.     }
  1229.  
  1230.   fclose (arcstream);
  1231. }
  1232.  
  1233. /* Write the __.SYMDEF member from data in core.  OUTDESC and OUTNAME
  1234.    are descriptor and name of file to write to.  */
  1235.  
  1236. void
  1237. write_symdef_member (mapelt, map, outdesc, outname)
  1238.      struct mapelt *mapelt;
  1239.      struct mapelt *map;
  1240.      int outdesc;
  1241.      char *outname;
  1242. {
  1243.   struct ar_hdr header;
  1244.   struct mapelt *mapptr;
  1245.   unsigned long int symdefs_size;
  1246.  
  1247.   if (mapelt->info.name == 0)
  1248.     /* This element was cancelled.  */
  1249.     return;
  1250.  
  1251.   header_from_map (&header, mapelt);
  1252.  
  1253.   bcopy (&header, &symdef_header, sizeof header);
  1254.  
  1255.   mywrite (outdesc, &header, sizeof (header), outname);
  1256.  
  1257.   /* Write the number of symdefs.  */
  1258.   symdefs_size = nsymdefs * sizeof (struct symdef);
  1259.   mywrite (outdesc, &symdefs_size, sizeof symdefs_size, outname);
  1260.  
  1261.   /* Write symdefs surviving from old archive.  */
  1262.   mywrite (outdesc, old_symdefs, num_old_symdefs * sizeof (struct symdef),
  1263.        outname);
  1264.  
  1265.   /* Write symdefs for new members.  */
  1266.   for (mapptr = map; mapptr != 0; mapptr = mapptr->next)
  1267.     if (mapptr->info.nsymdefs != 0)
  1268.       write (outdesc, mapptr->info.symdefs,
  1269.          mapptr->info.nsymdefs * sizeof (struct symdef));
  1270.  
  1271.   /* Write the string table size.  */
  1272.   mywrite (outdesc, &new_strings_size, sizeof new_strings_size, outname);
  1273.  
  1274.   /* Write the string table.  */
  1275.   mywrite (outdesc, new_strings, new_strings_size, outname);
  1276.  
  1277.   if (mapelt->info.size & 1)
  1278.     mywrite (outdesc, "", 1, outname);
  1279. }
  1280.  
  1281. /* Read various information from the header of an object file.
  1282.    Return 0 for failure or 1 for success.  */
  1283.  
  1284. int
  1285. read_header_info (mapelt, desc, offset, syms_offset, syms_size, strs_offset, strs_size)
  1286.      struct mapelt *mapelt;
  1287.      int desc;
  1288.      long int offset;
  1289.      long int *syms_offset;
  1290.      unsigned int *syms_size;
  1291.      long int *strs_offset;
  1292.      unsigned int *strs_size;
  1293. {
  1294. #ifdef A_OUT
  1295.   {
  1296.     struct exec hdr;
  1297.  
  1298.     lseek (desc, offset, 0);
  1299. #ifdef HEADER_SEEK_FD
  1300.     HEADER_SEEK_FD (desc);
  1301. #endif
  1302.  
  1303.     if (read (desc, (char *) &hdr, sizeof hdr) == sizeof hdr && !N_BADMAG(hdr))
  1304.       {
  1305.     *syms_offset = N_SYMOFF (hdr);
  1306.     *syms_size = hdr.a_syms;
  1307.     *strs_offset = N_STROFF (hdr);
  1308.     lseek (desc, N_STROFF (hdr) + offset, 0);
  1309.     if (read (desc, (char *) strs_size, sizeof *strs_size) != sizeof *strs_size)
  1310.       {
  1311.         error_with_file ("failure reading string table size in ", mapelt);
  1312.         return 0;
  1313.       }
  1314.     return 1;
  1315.       }
  1316.   }
  1317. #endif
  1318.  
  1319. #ifdef MACH_O
  1320.   {
  1321.     struct mach_header mach_header;
  1322.     struct load_command *load_command;
  1323.     struct symtab_command *symtab_command;
  1324.     char *hdrbuf;
  1325.     int cmd, symtab_seen;
  1326.  
  1327.     lseek (desc, offset, 0);
  1328.     if (read (desc, (char *) &mach_header, sizeof mach_header) == sizeof mach_header
  1329.     && mach_header.magic == MH_MAGIC)
  1330.       {
  1331.     hdrbuf = xmalloc (mach_header.sizeofcmds);
  1332.     if (read (desc, hdrbuf, mach_header.sizeofcmds) != mach_header.sizeofcmds)
  1333.       {
  1334.         error_with_file ("failure reading load commands of ", mapelt);
  1335.         return 0;
  1336.       }
  1337.     load_command = (struct load_command *) hdrbuf;
  1338.     symtab_seen = 0;
  1339.     for (cmd = 0; cmd < mach_header.ncmds; ++cmd)
  1340.       {
  1341.         if (load_command->cmd == LC_SYMTAB)
  1342.           {
  1343.         symtab_seen = 1;
  1344.         symtab_command = (struct symtab_command *) load_command;
  1345.         *syms_offset = symtab_command->symoff;
  1346.         *syms_size = symtab_command->nsyms * sizeof (struct nlist);
  1347.         *strs_offset = symtab_command->stroff;
  1348.         *strs_size = symtab_command->strsize;
  1349.           }
  1350.         load_command = (struct load_command *) ((char *) load_command + load_command->cmdsize);
  1351.       }
  1352.     free (hdrbuf);
  1353.     if (!symtab_seen)
  1354.       *syms_offset = *syms_size = *strs_offset = *strs_size = 0;
  1355.     return 1;
  1356.       }
  1357.   }
  1358. #endif
  1359.  
  1360.   error_with_file ("bad format (not an object file) in ", mapelt);
  1361.   return 0;
  1362. }
  1363.  
  1364. /* Create the info.symdefs for a new member
  1365.    by reading the file it is coming from.  */
  1366.  
  1367. void
  1368. make_new_symdefs (mapelt, archive_indesc)
  1369.      struct mapelt *mapelt;
  1370.      int archive_indesc;
  1371. {
  1372.   int indesc;
  1373.   char *name = mapelt->info.name;
  1374.   long int syms_offset, strs_offset;
  1375.   unsigned int syms_size, strs_size;
  1376.   struct nlist *symbols;
  1377.   int symcount;
  1378.   char *strings;
  1379.   register unsigned int i;
  1380.   unsigned long int offset;
  1381.  
  1382.   if (name == 0)
  1383.     /* Deleted member.  */
  1384.     abort ();
  1385.  
  1386.     {
  1387.       indesc = open (mapelt->info.name, 0, 0);
  1388.       if (indesc < 0)
  1389.     {
  1390.       perror_with_name (mapelt->info.name);
  1391.       return;
  1392.     }
  1393.       offset = 0;
  1394.     }
  1395.  
  1396.   if (!read_header_info (mapelt, indesc, offset, &syms_offset, &syms_size, &strs_offset, &strs_size))
  1397.     {
  1398.       if (mapelt->info.offset == 0)
  1399.     close (indesc);
  1400.       return;
  1401.     }
  1402.  
  1403.   /* Number of symbol entries in the file.  */
  1404.   symcount = syms_size / sizeof (struct nlist);
  1405.   /* Allocate temporary space for the symbol entries.  */
  1406.   symbols = (struct nlist *) alloca (syms_size);
  1407.   /* Read in the symbols.  */
  1408.   lseek (indesc, syms_offset + offset, 0);
  1409.   if (read (indesc, (char *) symbols, syms_size) != syms_size)
  1410.     {
  1411.       error_with_file ("premature end of file in symbols of ", mapelt);
  1412.       if (mapelt->info.offset == 0)
  1413.     (void) close (indesc);
  1414.       return;
  1415.     }
  1416.  
  1417.   /* The string table size includes the size word.  */
  1418.   if (strs_size < sizeof (strs_size))
  1419.     {
  1420.       error_with_file ("bad string table size in ", mapelt);
  1421.       if (mapelt->info.offset == 0)
  1422.     (void) close (indesc);
  1423.       return;
  1424.     }
  1425.   strs_size -= sizeof (strs_size);
  1426.  
  1427.   /* Allocate permanent space for the string table.  */
  1428.   strings = (char *) xmalloc (strs_size);
  1429.  
  1430.   /* Read in the strings.  */
  1431.   lseek (indesc, offset + strs_offset + sizeof strs_size, 0);
  1432.   if (read (indesc, strings, strs_size) != strs_size)
  1433.     {
  1434.       error_with_file ("premature end of file in strings of ", mapelt);
  1435.       if (mapelt->info.offset == 0)
  1436.     (void) close (indesc);
  1437.       return;
  1438.     }
  1439.  
  1440.     (void) close (indesc);
  1441.  
  1442.   /* Discard the symbols we don't want to mention; compact the rest down.  */
  1443.   symcount = filter_symbols (symbols, symcount);
  1444.  
  1445.   mapelt->info.symdefs = (struct symdef *)
  1446.     xmalloc (symcount * sizeof (struct symdef));
  1447.   mapelt->info.nsymdefs = symcount;
  1448.   mapelt->info.string_size = 0;
  1449.  
  1450.   for (i = 0; i < symcount; ++i)
  1451.     {
  1452.       unsigned long int stroff = symbols[i].n_un.n_strx - sizeof (strs_size);
  1453.       char *symname = strings + stroff;
  1454.       if (stroff > strs_size)
  1455.     {
  1456.       char buf[100];
  1457.       sprintf (buf, "ridiculous string offset %lu in symbol %u of ",
  1458.            stroff + sizeof (strs_size), i);
  1459.       error_with_file (buf, mapelt);
  1460.       return;
  1461.     }
  1462.       mapelt->info.symdefs[i].s.name = symname;
  1463.       mapelt->info.string_size += strlen (symname) + 1;
  1464.     }
  1465. }
  1466.  
  1467. /* Choose which symbol entries to mention in __.SYMDEF;
  1468.    compact them downward to get rid of the rest.
  1469.    Return the number of symbols left.  */
  1470.  
  1471. int
  1472. filter_symbols (syms, symcount)
  1473.      struct nlist *syms;
  1474.      unsigned int symcount;
  1475. {
  1476.   struct nlist *from, *to;
  1477.   struct nlist *end = syms + symcount;
  1478.  
  1479.   for (to = from = syms; from < end; ++from)
  1480.     if ((from->n_type & N_EXT)
  1481.     && (from->n_type != N_EXT || from->n_value != 0))
  1482.       *to++ = *from;
  1483.  
  1484.   return to - syms;
  1485. }
  1486.  
  1487.  
  1488. /* Update the __.SYMDEF data before writing a new archive.  */
  1489.  
  1490. void
  1491. update_symdefs (map, archive_indesc)
  1492.      struct mapelt *map;
  1493.      int archive_indesc;
  1494. {
  1495.   struct mapelt *tail;
  1496.   int pos;
  1497.   register unsigned int i;
  1498.   unsigned int len;
  1499.   struct symdef *s;
  1500.   unsigned long int deleted_strings_size = 0;
  1501.  
  1502.   nsymdefs = original_num_symdefs;
  1503.   num_old_symdefs = original_num_symdefs;
  1504.   new_strings_size = old_strings_size;
  1505.   if (nsymdefs != 0)
  1506.     {
  1507.       /* We already had a __.SYMDEF member, so just update it.  */
  1508.  
  1509.       /* Mark as canceled any old symdefs for members being deleted.  */
  1510.  
  1511.       for (tail = map; tail != 0; tail = tail->next)
  1512.     {
  1513.       if (tail->info.name == 0)
  1514.         {
  1515.           /* Old member being deleted.  Delete its symdef entries too.  */
  1516.           for (i = 0; i < original_num_symdefs; i++)
  1517.         if (old_symdefs[i].offset == tail->info.offset)
  1518.           {
  1519.             old_symdefs[i].offset = 0;
  1520.             --nsymdefs;
  1521.             deleted_strings_size
  1522.               += strlen (old_strings
  1523.                  + old_symdefs[i].s.stringoffset) + 1;
  1524.           }
  1525.         }
  1526.     }
  1527.  
  1528.       /* Compactify old symdefs.  */
  1529.       {
  1530.     register unsigned int j = 0;
  1531.     for (i = 0; i < num_old_symdefs; ++i)
  1532.       {
  1533.         if (j != i)
  1534.           old_symdefs[j] = old_symdefs[i];
  1535.         if (old_symdefs[i].offset != 0)
  1536.           ++j;
  1537.       }
  1538.     num_old_symdefs -= i - j;
  1539.       }
  1540.  
  1541.       /* Create symdef data for any new members.  */
  1542.       for (tail = map; tail != 0; tail = tail->next)
  1543.     {
  1544.       if (tail->info.offset != 0
  1545.           || tail->info.name == 0
  1546.           || !strcmp (tail->info.name, "__.SYMDEF"))
  1547.         continue;
  1548.       make_new_symdefs (tail, archive_indesc);
  1549.       nsymdefs += tail->info.nsymdefs;
  1550.       new_strings_size += tail->info.string_size;
  1551.     }
  1552.     }
  1553.   else
  1554.     {
  1555.       /* Create symdef data for all existing members.  */
  1556.  
  1557.       for (tail = map; tail != 0; tail = tail->next)
  1558.     {
  1559.       if (tail->info.name == 0
  1560.           || !strcmp (tail->info.name, "__.SYMDEF"))
  1561.         continue;
  1562.       make_new_symdefs (tail, archive_indesc);
  1563.       nsymdefs += tail->info.nsymdefs;
  1564.       new_strings_size += tail->info.string_size;
  1565.     }
  1566.     }
  1567.  
  1568.   new_strings_size -= deleted_strings_size;
  1569.   old_strings_size -= deleted_strings_size;
  1570.  
  1571.   /* Now we know the size of __.SYMDEF,
  1572.      so assign the positions of all the members.  */
  1573.  
  1574.   tail = find_mapelt_noerror (map, "__.SYMDEF");
  1575.   tail->info.size = (sizeof (nsymdefs) + (nsymdefs * sizeof (struct symdef))
  1576.              + sizeof (new_strings_size) + new_strings_size);
  1577.   symdef_mapelt = tail;
  1578.  
  1579.   pos = SARMAG;
  1580.   for (tail = map; tail != 0; tail = tail->next)
  1581.     {
  1582.       if (tail->info.name == 0)
  1583.     /* Ignore deleted members.  */
  1584.     continue;
  1585.       tail->info.new_offset = pos;
  1586.       pos += sizeof (struct ar_hdr) + tail->info.size;
  1587.       if (tail->info.size & 1)
  1588.     ++pos;
  1589.     }
  1590.  
  1591.   /* Now update the offsets in the symdef data
  1592.      to be the new offsets rather than the old ones.  */
  1593.  
  1594.   for (tail = map; tail != 0; tail = tail->next)
  1595.     {
  1596.       if (tail->info.name == 0)
  1597.     continue;
  1598.       if (tail->info.symdefs == 0)
  1599.     /* Member without new symdef data.
  1600.        Check the old symdef data; it may be included there. */
  1601.     for (i = 0; i < num_old_symdefs; i++)
  1602.       {
  1603.         if (old_symdefs[i].offset == tail->info.offset)
  1604.           old_symdefs[i].offset = tail->info.new_offset;
  1605.       }
  1606.       else
  1607.     for (i = 0; i < tail->info.nsymdefs; i++)
  1608.       tail->info.symdefs[i].offset = tail->info.new_offset;
  1609.     }
  1610.  
  1611.   /* Generate new, combined string table and put each string's offset into the
  1612.      symdef that refers to it.  Note that old symdefs ref their strings by
  1613.      offsets into old_strings but new symdefs contain addresses of strings.  */
  1614.  
  1615.   new_strings = (char *) xmalloc (new_strings_size);
  1616.   pos = 0;
  1617.  
  1618.   /* Write the strings of the old symdefs and update the structures
  1619.      to contain indices into the string table instead of strings.  */
  1620.   for (i = 0; i < num_old_symdefs; i++)
  1621.     {
  1622.       strcpy (new_strings + pos, old_strings + old_symdefs[i].s.stringoffset);
  1623.       old_symdefs[i].s.stringoffset = pos;
  1624.       pos += strlen (new_strings + pos) + 1;
  1625.     }
  1626.   if (pos < old_strings_size)
  1627.     {
  1628.       unsigned int d = old_strings_size - pos;
  1629.       /* Correct the string table size.  */
  1630.       new_strings_size -= d;
  1631.       /* Correct the size of the `__.SYMDEF' member,
  1632.      since it contains the string table.  */
  1633.       symdef_mapelt->info.size -= d;
  1634.     }
  1635.   else if (pos > old_strings_size)
  1636.     fatal ("Old archive's string size was %u too small.",
  1637.        pos - old_strings_size);
  1638.  
  1639.   for (tail = map; tail != 0; tail = tail->next)
  1640.     if (tail->info.symdefs)
  1641.       {
  1642.     len = tail->info.nsymdefs;
  1643.     s = tail->info.symdefs;
  1644.  
  1645.     for (i = 0; i < len; i++)
  1646.       {
  1647.         strcpy (new_strings + pos, s[i].s.name);
  1648.         s[i].s.stringoffset = pos;
  1649.         pos += strlen (new_strings + pos) + 1;
  1650.       }
  1651.       }
  1652.   if (pos != new_strings_size)
  1653.     fatal ("internal error: inconsistency in new_strings_size", 0);
  1654. }
  1655.  
  1656. /* Print error message and usage message, and exit.  */
  1657.  
  1658. void
  1659. usage (s1, s2)
  1660.      char *s1, *s2;
  1661. {
  1662.   error (s1, s2);
  1663.   fprintf (stderr, "\
  1664. Usage: %s [d|m|p|q|r|t|x [[abi [position-name] [cilouv]] archive file...\n",
  1665.        program_name);
  1666.   exit (1);
  1667. }
  1668.  
  1669. /* Print error message and exit.  */
  1670.  
  1671. void
  1672. fatal (s1, s2)
  1673.      char *s1, *s2;
  1674. {
  1675.   error (s1, s2);
  1676.   exit (1);
  1677. }
  1678.  
  1679. /* Print error message.  `s1' is printf control string, the rest are args.  */
  1680.  
  1681. void
  1682. error (s1, s2, s3, s4, s5)
  1683.      char *s1, *s2, *s3, *s4, *s5;
  1684. {
  1685.   fprintf (stderr, "%s: ", program_name);
  1686.   fprintf (stderr, s1, s2, s3, s4, s5);
  1687.   fprintf (stderr, "\n");
  1688. }
  1689.  
  1690. void
  1691. error_with_file (string, mapelt)
  1692.      char *string;
  1693.      struct mapelt *mapelt;
  1694. {
  1695.   fprintf (stderr, "%s: ", program_name);
  1696.   fprintf (stderr, string);
  1697.   if (mapelt->info.offset != 0)
  1698.     fprintf (stderr, "%s(%s)", archive, mapelt->info.name);
  1699.   else
  1700.     fprintf (stderr, "%s", mapelt->info.name);
  1701.   fprintf (stderr, "\n");
  1702. }
  1703.  
  1704. void
  1705. perror_with_name (name)
  1706.      char *name;
  1707. {
  1708.   extern int errno, sys_nerr;
  1709.   extern char *sys_errlist[];
  1710.   char *s;
  1711.  
  1712.   if (errno < sys_nerr)
  1713.     s = concat ("", sys_errlist[errno], " for %s");
  1714.   else
  1715.     s = "unknown error for %s";
  1716.   error (s, name);
  1717. }
  1718.  
  1719. void
  1720. pfatal_with_name (name)
  1721.      char *name;
  1722. {
  1723.   extern int errno, sys_nerr;
  1724.   extern char *sys_errlist[];
  1725.   char *s;
  1726.  
  1727.   if (errno < sys_nerr)
  1728.     s = concat ("", sys_errlist[errno], " for %s");
  1729.   else
  1730.     s = "cannot open %s";
  1731.   fatal (s, name);
  1732. }
  1733.  
  1734. /* Return a newly-allocated string whose contents
  1735.    concatenate those of S1, S2, and S3.  */
  1736.  
  1737. char *
  1738. concat (s1, s2, s3)
  1739.      char *s1, *s2, *s3;
  1740. {
  1741.   int len1 = strlen (s1), len2 = strlen (s2), len3 = strlen (s3);
  1742.   char *result = (char *) xmalloc (len1 + len2 + len3 + 1);
  1743.  
  1744.   strcpy (result, s1);
  1745.   strcpy (result + len1, s2);
  1746.   strcpy (result + len1 + len2, s3);
  1747.   *(result + len1 + len2 + len3) = 0;
  1748.  
  1749.   return result;
  1750. }
  1751.  
  1752. #ifndef THINK_C
  1753. /* Like malloc but get fatal error if memory is exhausted.  */
  1754.  
  1755. char *
  1756. xmalloc (size)
  1757.      unsigned int size;
  1758. {
  1759.   extern char *malloc ();
  1760.   char *result = malloc (size);
  1761.   if (result == 0)
  1762.     fatal ("virtual memory exhausted", 0);
  1763.   return result;
  1764. }
  1765.  
  1766. char *
  1767. xrealloc (ptr, size)
  1768.      char *ptr;
  1769.      unsigned int size;
  1770. {
  1771.   extern char *realloc ();
  1772.   char *result = realloc (ptr, size);
  1773.   if (result == 0)
  1774.     fatal ("virtual memory exhausted");
  1775.   return result;
  1776. }
  1777.  
  1778. #endif
  1779.